<?php
class Tourismtemplet extends CS_Controller
{
    public function _init()
    {
        $this->load->helper('common');
        $this->load->library('pagination');
        $this->load->model('region_model', 'region');
        $this->load->model('tourism_freight_free_model','tourism_freight_free');
        $this->load->model('tourism_freight_template_model', 'tourism_freight_template');
        $this->load->model('tourism_freight_price_list_model', 'tourism_freight_price_list');
    }

    /**
     * 运费模板列表
     * @param number $pg
     */
    public function grid($pg = 1)
    {
        $getData = $this->input->get();
        $page_num = 20;
        $num = ($pg - 1) * 20;
        $config['first_url'] = base_url('tourismtemplet/grid') . $this->pageGetParam($getData);
        $config['suffix'] = $this->pageGetParam($getData);
        $config['base_url'] = base_url('tourismtemplet/grid');
        $config['total_rows'] = $this->tourism_freight_template->total($getData);
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);
        $data['pg_list'] = $this->pagination->create_links();
        $data['list'] = $this->tourism_freight_template->page_list($page_num, $num, $getData);
        $data['all_rows'] = $config['total_rows'];
        $data['pg_now'] = $pg;
        $data['methods'] = array('1'=>'件','2'=>'重量','3'=>'体积');
        $data['url'] = $this->pageGetParam($this->input->get());
        $this->load->view('tourismtemplet/grid', $data);
    }

    /*
     *运费模板列表
     */
    public function add()
    {
        $data['deliver_time'] = array(
        	'4' => '4小时内',
            '8' => '8小时内',
            '12' => '12小时内',
            '16' => '16小时内',
            '20' => '20小时内',
            '24' => '24小时内',
            '48' => '48小时内'
        );
        $this->load->view('tourismtemplet/add',$data);
    }

    public function ajaxGetTemplet()
    {
        $data['regionAll'] = getRegionAll();
        echo json_encode(array(
            'status' => true,
            'html' => $this->load->view('tourismtemplet/templet/ajaxTempletDate', $data, true)
        )); exit;
    }

    public function addPost()
    {   
        $error = $this->validate($this->input->post());
        if (!empty($error)) {
            $this->jsonMessage($error);
        }
        $template_param['name'] = $this->input->post('name', true);
        $template_param['logistics'] = (int)$this->input->post('logistics');
        $template_param['methods'] = (int)$this->input->post('methods');
        $template_param['uid'] = (int)$this->input->post('uid');
        $template_param['deliver_time'] = (int)$this->input->post('deliver_time');
        
        $default_params['area'] = '';
        $default_params['first_unit'] = $this->input->post('unit_first');
        $default_params['first_price'] = $this->input->post('price_first');
        $default_params['add_unit'] = $this->input->post('unit_add');
        $default_params['add_price'] = $this->input->post('price_add');
        $default_params['is_default'] = 1;
        
        $free_params['free_area'] = $this->input->post('free_area');
        $free_params['uid'] = (int)$this->input->post('uid');
        $free_params['methods'] = (int)$this->input->post('methods');
        $free_params['number'] = $this->input->post('num') ? $this->input->post('num') : 0;
        $free_params['money'] = $this->input->post('money') ? $this->input->post('money') : 0;
        $free_params['free_type'] = (int)$this->input->post('free_type');
        
        $list_params = $this->input->post('list') ? $this->input->post('list') : array();
        if (is_array($list_params)) {
            $list_params[] = $default_params;
        }
        $this->db->trans_start();
        $resultId = $this->tourism_freight_template->add($template_param, $list_params);
        if (!empty($free_params['free_area'])) {
            $free_params['tid'] = $resultId;
        	$this->tourism_freight_free->insert($free_params);
        }
        $this->db->trans_complete();
        if ($this->db->trans_status()===TRUE) {
            $this->session->set_flashdata('success','保存成功');
            $this->jsonMessage('','tourismtemplet/grid');
        } 
        $this->jsonMessage('保存失败！');
    }

    public function delete($id)
    {
        $id = (int)$id;
        if (!$id) {
            $this->error('tourismtemplet/grid', '', '参数有误！');
        }
        $this->db->trans_start();
        $this->tourism_freight_template->delete($id);
        $this->db->trans_complete();
        if ($this->db->trans_status()===TRUE) {
            $this->success('tourismtemplet/grid', '', '删除成功！');
        } else {
            $this->error('tourismtemplet/grid', '', '删除失败！');
        }
    }

    public function edit($id = 0)
    {
        if ($id == 0) {
            $this->error('tourismtemplet/grid', '', '参数有误');
        }
        $result = $this->tourism_freight_template->getTemplate($id);
        $default_freight = $this->tourism_freight_price_list->getFreight($id, 1);

        $data['result'] = $result->row();
        $data['deliver_time'] = array(
                '4' => '4小时内',
                '8' => '8小时内',
                '12' => '12小时内',
                '16' => '16小时内',
                '20' => '20小时内',
                '24' => '24小时内',
                '48' => '48小时内'
        );
        $data['default_freight'] = $default_freight->row();
        $this->load->view('tourismtemplet/edit', $data);
    }

    public function ajax($id)
    {
        $freight = $this->tourism_freight_price_list->getFreight($id, 0);
        echo json_encode($freight->result());
        exit;
    }
    
    public function ajaxFree($id)
    {
        $free = $this->tourism_freight_free->getFreightFree($id);
        echo json_encode($free->result());exit;
    }
    
    public function editPost()
    {
        $error = $this->validate($this->input->post());
        if (!empty($error)) {
            $this->jsonMessage($error);
        }
        $template_id = (int)$this->input->post('template');
        
        $template_param['name'] = $this->input->post('name', true);
        $template_param['logistics'] = (int)$this->input->post('logistics');
        $template_param['methods'] = (int)$this->input->post('methods');
        $template_param['uid'] = (int)$this->input->post('uid');
        $template_param['deliver_time'] = (int)$this->input->post('deliver_time');
        
        $default_params['area'] = '';
        $default_params['first_unit'] = $this->input->post('unit_first');
        $default_params['first_price'] = $this->input->post('price_first');
        $default_params['add_unit'] = $this->input->post('unit_add');
        $default_params['add_price'] = $this->input->post('price_add');
        $default_params['is_default'] = 1;

        $free_params['free_area'] = $this->input->post('free_area');
        $free_params['uid'] = (int)$this->input->post('uid');
        $free_params['methods'] = (int)$this->input->post('methods');
        $free_params['number'] = $this->input->post('num') ? $this->input->post('num') : 0;
        $free_params['money'] = $this->input->post('money') ? $this->input->post('money') : 0;
        $free_params['free_type'] = (int)$this->input->post('free_type');
        $free_params['tid'] = $template_id;
        
        $list_params = $this->input->post('list') ? $this->input->post('list') : array();
      
        if (is_array($list_params)) {
            $list_params[] = $default_params;
        }
        if (!$template_id) {
            $this->jsonMessage('参数有误！');
        }
        $free = $this->tourism_freight_free->getFreightFree($template_id);
        $this->db->trans_start();
        $resultId = $this->tourism_freight_template->update($template_id, $template_param, $list_params);
        if ($free->num_rows()>0) {
            if (empty($free_params['free_area'])) {
                $this->tourism_freight_free->deleteByTid($template_id);
            } else {
                $this->tourism_freight_free->updateByTid($template_id,$free_params);
            }
        } else {
            if (!empty($free_params['free_area'])) {
                $this->tourism_freight_free->insert($free_params);
            }
        }
        $this->db->trans_complete();
        if ($this->db->trans_status()===TRUE) {
            $this->session->set_flashdata('success','修改成功！');
            $this->jsonMessage('','tourismtemplet/grid');
        }
        $this->jsonMessage('修改失败！');
    }

     /**
     * 获取运费模板信息
     * author laona
     **/
    public function ajaxGetTransport()
    {
        $uid = $this->input->post('uid');
        $result = $this->tourism_freight_template->getTransport($uid);

        echo json_encode($result->result());
        exit;
    }
    
     /**
     *验证
     * @param unknown $param
     */
    public function validate($param)
    {
        $error = array();
        if ( !empty($param['free_flag']) ) {
            
            if ( empty($param['free_area']) ){
                $error[] = '免费地址不能为空';
            }
            if ( !empty($param['free_type']) && ($param['free_type'] == 1) ) {
                if ( empty($param['num']) || ($param['num'] < 1) ) {
                    $error[] = '数量不能小于1';
                }
            }
            if ( !empty($param['free_type']) && ($param['free_type'] == 2) ) {
                if ( empty($param['money']) || ($param['money']<0) ) {
                    $error[] = '金额不能小于零';
                }
            }
        }
        return $error;
    }
}